package cn.itcast.bos.web.action.system;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.bos.web.action.common.BaseAction;

public class MenuAction extends BaseAction<Menu>{
	@Resource
	private MenuService service;
	
	@Action(value = "menu_list", results = { @Result(name = "success", type = "json") })
	public String menu_list(){
		List<Menu> menus = service.findByParentMenuIsNull();
		ActionContext.getContext().getValueStack().push(menus);
		return SUCCESS;
	}
	
	@Action(value = "menu_save", results = { @Result(name = "success", type = "redirect",location="pages/system/menu.html") })
	public String save(){
		service.save(model);
		return SUCCESS;
	}
	
	@Action(value="menu_showmenu")
	public String menu_showmenu() throws Exception{
		//获取当前登录用户信息
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		List<Menu> list = service.findByUser(user);
		
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(list);
		
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);
		return NONE;
	}
}
